package com.mysteel.cyx.demo.biz.export;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;



@NoArgsConstructor
public class CustomCellWriteHandler implements CellWriteHandler {

    private Integer businessDate;

    public CustomCellWriteHandler(Integer businessDate) {
        this.businessDate = businessDate;
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        Sheet sheet = cell.getSheet();
        Workbook workbook = sheet.getWorkbook();

        Integer rowIndex = context.getRowIndex();
        Integer columnIndex = context.getColumnIndex();
        // 第一行和第二行合并并居中显示
        if (rowIndex == 0 && columnIndex == 0) {
            mergeAndCenter(sheet, 0, true, "监控日报");
        } else if (rowIndex == 1 && columnIndex == 0) {
            mergeAndCenter(sheet, 1, false, "业务日期：" + businessDate);
        }
    }






    private void mergeAndCenter(Sheet sheet, int rowNumber, boolean isBold, String text) {
        Workbook workbook = sheet.getWorkbook();
        CellStyle cellStyle = ExcelUtil.createBoldCenterCellStyle(workbook, isBold ? (short) 12 : (short) 11, isBold);
        Row row = sheet.createRow(rowNumber);
        Cell cell = row.createCell(0);
        cell.setCellValue(text);
        cell.setCellStyle(cellStyle);
        sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, 16));
    }

}
